Compound queries可以包裝其他的複合查詢以及leaf queries
讓我們可以在查詢時進行更多的邏輯判斷與結果篩選
包含以下幾種類型:
Boolean Query:
主要由幾種參數來進行判斷:
GET /index_name/_search
{
  "query": {
    "bool" : {
      "must" : {
        "field_name" : "value"
      },
      "filter": {
        "field_name" : "value"
      },
      "must_not" : {
        "range" : {
          "create_time" : { "gte" : "2021", "lte" : "2023" }
        }
      },
      "should" : [
        { "term" : { "field_name" : "value" } }
      ],
      "minimum_should_match" : 1
    }
  }
}
Boosting Query:
GET /index_name/_search
{
  "query": {
    "boosting": {
      "positive": {
        "must": [
					"match_all": {}
				]
      },
      "negative": {
        "term": {
          "text": ""
        }
      },
      "negative_boost": 0.5
    }
  }
}
組合拳: 如果我今天想要title欄位出現有關咖啡的字樣,但是我更希望能出現有關淺焙的品項,最好避開深焙的豆子
GET /bool_and_boost/_search
{
  "query": {
    "boosting": {
      "positive": {
        "bool": {
          "must": [ // 標題要包含coffee
            {
              "match": {
                "title": "coffee"
              }
            }
          ],
          "should": [ // 如果是淺焙會加分
            {
              "term": {
                "category": "light"
              }
            }
          ]
        }
      },
      "negative": {  // 如果是深焙會扣分
        "match": {
          "category": "dark"
        }
      },
      "negative_boost": 0.5
    }
  }
}
Constant_score query:
GET /_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": { "user.id": "kimchy" }
      },
      "boost": 1.2
    }
  }
}
Dis_max query:
GET /dis_max/_search
{
  "query": {
    "dis_max": {
      "tie_breaker": 0.7,
      "queries": [
        {
          "match": {
            "FIELD_1": "TEXT"
          }
        },
        {
          "match": {
            "FIELD_2": "TEXT"
          }
        }
      ]
    }
  }
}
Function score query:
GET /_search
{
  "query": {
    "function_score": {
      "query": { "match_all": {} }, //查詢語句,提供原始分數
      "boost": "5", // 提供基礎分數 
      "random_score": {}, // 我們要另外計算的方式 
      "boost_mode": "multiply" // 代表拿到原始分數與額外計算分數後,最終分數的處理方式
    }
  }
}
Script_score:
GET /_search
{
  "query": {
    "function_score": {
      "query": {
        "match": { "message": "elasticsearch" }
      },
      "script_score": {
        "script": {
          "source": "Math.log(2 + doc['my-int'].value)"
        }
      }
    }
  }
}
Weight:
"weight" : number
Random_score:
GET /_search
{
  "query": {
    "function_score": {
      "random_score": {
        "seed": 10,
        "field": "_seq_no"
      }
    }
  }
}
Field_value_factor:
GET /_search
{
  "query": {
    "function_score": {
      "field_value_factor": {
        "field": "my-int", // 選取作為額外計算的欄位
        "factor": 1.2, // 預設為1,會*上面的field
        "modifier": "sqrt", // 會將上面經由factor算完的值再進行處理,這邊是算平方根
        "missing": 1 // 如果field沒有值,就拿來當作field的值
      }
    }
  }
}
Decay functions:
GET /_search
{
  "query": {
    "function_score": {
      "gauss": { // 定義使用的decay函式類型,可以選擇gauss, exp跟linear
        "@timestamp": {
          "origin": "2023-09-17", 
          "scale": "10d",
          "offset": "5d",          
          "decay": 0.5            
        }
      }
    }
  }
}
// 這樣的範例代表,在2023-09-12到2023-9-22的區間範圍的文檔們,距離他們10天以上的文檔就必須*0.5
可能這樣介紹下來function score query比較亂,這邊再統整一次
今天我們介紹了ES強大的compound query
參考資料
boolean query:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html
filter context:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html
boosting query:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-boosting-query.html
constant score query:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html
function score query:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html